import { reqUserInfo } from '@/apis/mine/user'
import { getToken, setToken } from '@/utils/token'
import {defineStore} from 'pinia'

export const useUserInfoStore = defineStore("userInfo",{
    state: () => ({
        token:getToken() as string,
        userInfo:localStorage.getItem('userInfo')? JSON.parse(localStorage.getItem('userInfo') as any):{
            account:"",
            avatar:""
        }   
    }),

    actions: {
        async login() {
            try {
                const re = await reqUserInfo()
                console.log(re)
                this.userInfo.avatar = re.avatar
                this.userInfo.account = re.account
                setToken()
               localStorage.setItem('userInfo',JSON.stringify(this.userInfo))            
            }catch(e) {
                return Promise.reject(e)
            }
        }
    }
})